package aes.server;

import java.sql.*;
import java.util.*;

import common.Subject;

/**
 * This class represents manager used in order to manage subjects
 *
 */
public class SubjectsManager {

	/**
	 * Retrieves subjects list from the DB
	 * @return Subject objects array
	 */
	public static Subject[] getSubjects() {
		ArrayList<Subject> arr=new ArrayList<Subject>();
		DBManager db = DBManager.getInstance();
		try {
			String sql ="SELECT dId, dName FROM departments";
			PreparedStatement stmt1 = db.getConnection().prepareStatement(sql);
			ResultSet rs1 = stmt1.executeQuery();
			while(rs1.next()){
				Subject d = new Subject();
				d.setId(rs1.getInt("dId"));
				d.setName(rs1.getString("dName"));
				arr.add(d);
			}
			Subject[] ret = new Subject[arr.size()];
			arr.toArray(ret);
			return ret;
		}
		catch(SQLException ex){
			return null;
		}
	}
	
}
